package com.vtech.ws.cxf.rs;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.cxf.jaxrs.ext.multipart.Attachment;
import org.apache.cxf.jaxrs.ext.multipart.Multipart;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.File;
import java.util.List;

/**
 * https://www.javatips.net/blog/cxf-rest-file-upload
 * https://blog.csdn.net/wodewutai17quiet/article/details/53104034
 * http://cxf.apache.org/docs/jax-rs-multiparts.html
 * https://www.javatips.net/blog/download-pdf-file-using-cxf-rest-jax-rs
 *
 * @author houzhiwei
 * @date 2018 /9/4 17:23
 */
@Api("File REST Web Services")
@Path("/file")
public interface FileService {


    /**
     * Upload a big file.
     *
     * @param targetFile the target file
     * @param username   the username
     * @return the string
     */
    @POST
    @Path("/upload")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation("Upload a big file")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "targetFile", dataTypeClass = File.class, paramType = "form",required = true),
            @ApiImplicitParam(name = "user", dataTypeClass = String.class, value = "username", paramType = "form")
    })
    public Response uploadFile(@Multipart(value = "targetFile") @NotNull Attachment targetFile,
                               @Multipart(value = "user", type = "text/plain") String username);

    /**
     * Upload small file (such as image, txt, shapefile etc.).
     *
     * @param request the request
     * @return the string
     */
    @POST
    @Path("/uploadSmall")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation("Upload small file (such as image, txt, shapefile etc.)")
    public Response uploadSmallFile(@Context HttpServletRequest request);


    /**
     * Upload multiple files.
     *
     * @param attachments the attachments
     * @return the response
     */
    @POST
    @Path("/uploadFiles")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response uploadFiles(List<Attachment> attachments, @Context HttpServletRequest request);

    /**
     * Download a file.
     * <p>
     * //@param filename the filename @QueryParam(value = "filename") String filename
     *
     * @return the response
     */
    @GET
    @Path("/download")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_OCTET_STREAM)
    public Response download();


    /**
     * Export excel file.
     * multipart/form-data
     *
     * @param name     the name
     * @param response the response
     * @return the response
     */
    @GET
    @Path("/export")
    @Produces("application/vnd.ms-excel")
    public Response exportFile(@FormParam("name") String name, @Context HttpServletResponse response);


}
